<style scoped>
</style>
<template>
  <template v-for="(row,i) in frame_list">
    <van-popup  v-model="row.show" closeable :width="row.width" style="height: 90vh;margin: 5vh auto;" :destroy-on-close="true" @closed="frame_list.splice(i,1)">
      <component :is="row.component_name" :ref="row.unique" :__iframe="row" ></component>
    </van-popup>
  </template>
</template>

<script>

import {decode_btns, eq} from "@/js/s94_tool.js";
import s94Mjs from "s94-js";
import {view_component} from "@/vue_plugin/router_auto.js";

export default {
  components: {},
  data(){
    return {
      is_show: false,
      callback(ac){},

      frame_list: [],
      time: 0,
      icon: 'vant-icon gift',
      type: "", // 需要滚动才计时|need_scroll

      is_scroll: false, //是否正在滚动
      is_over: false, //是否计时完成
      is_hidden: false,

      surplus_time: 0, //剩余时间
      progress_width: 0, // 进度条宽度(0-100)

    }
  },
  methods: {
    show(config, callback){
      this.is_show = true;
      if (typeof config === 'string') config = {path:config};
      if (typeof config !== 'object') config = {};
      if (typeof callback === 'function') this.callback = callback;

      /**
       *
       * @type {{ref_name: string, path, param: {}, show: boolean, width: string, btns: Array}}
       */
      let frame_row = {
        show: true,
        ref_name: 'frame'+(new Date()).getTime(),
        path: config.path,
        btns: decode_btns(config.btns || []),
        width: config.width || '80%',
        param: typeof config.param ==='object' ? config.param : {},
      }
      // 路径参数剥离
      if (~frame_row.path.indexOf('?')){
        let arr = frame_row.path.split('?');
        frame_row.param = Object.assign(frame_row.param, arr[1] ? s94.param_decode(arr[1]) : {});
        frame_row.path = arr[0];
      }
      frame_row.path = s94Mjs.trim(frame_row.path, '/');

      if (!(frame_row.path in this.$options.components)) this.$options.components[frame_row.path] = view_component(frame_row.path);
      this.frame_list.push(frame_row);

      return this;
    },
    hide(path){
      this.find_frame_list(path).forEach(function (row){
        row.show = false;
      })
    },
    init(){
      // 关闭全部框架
      this.frame_list.forEach(row=>{
        row.show = false;
      })
    },


    find_frame_list(path, frame_list){
      frame_list = frame_list || this.frame_list;
      let path_param = {};
      if (~path.indexOf('?')){
        let arr = path.split('?');
        path_param = arr[1] ? s94.param_decode(arr[1]) : {};
        path = arr[0];
      }
      path = s94Mjs.trim(path, '/');
      let res = [];
      frame_list.forEach(frame_row=>{
        if (frame_row.path !== path) return;
        if (!eq(path_param, frame_row.param)) return; //指定的路径参数必须和显示框架时参数相同。例如：goods/index?a=a 只能匹配 goods/index?a=a
        res.push(frame_row);
      })

      return res;
    },
    exe(path, function_name, param){
      let key_arr = function_name.replace(/^(app|this)\./,'').split('.');
      let app_list = [];
      this.find_frame_list(path).forEach((row)=>{
        this.$refs[row.unique] && app_list.push(this.$refs[row.unique][0]);
      })

      app_list.forEach((res)=>{
        for (let i = 0; i < key_arr.length; i++) {
          let k = key_arr[i];
          if (!res[k]) break;
          if (i >= key_arr.length-1){
            try {
              res[k].apply(res, param);
            } catch (e) {
              console.error('iframe.exe执行错误:', e)
            }
          }else {
            res = res[k];
          }
        }
        s94.each(key_arr, function (k, i) {
          if (!res[k]) return false;
          if (i >= key_arr.length-1){
            //执行方法
            try {
              res[k].apply(res, param);
            } catch (e) {
              console.error('iframe.exe执行错误:', e)
            }
          }else {
            res = res[k];
          }
        })
      })
    },
    action(ac){
      if (typeof this.callback !== 'function') return;
      this.callback(this, ac);
    },
    refresh_time(){
      let run = Math.round(this.stopwatch/1000);
      this.progress_width = Math.min(Math.round(run*100/this.time), 100);
      this.surplus_time = Math.max(this.time - run, 0);

      if (this.is_show) setTimeout(this.refresh_time, 500);
    },
    on_click(){
      this.action('click');
      if (!this.is_over) return;
      this.is_hidden = false;
      setTimeout(()=>{ this.is_hidden = this.is_over; }, 2000)
    }
  },
  watch: {
  },
  mounted() {
    window["test_show"] = this.show;
  }
}
</script>